package com.woniu.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.Department;
import com.woniu.entity.MaintenancePerson;
import com.woniu.service.MaintenancePersonService;
import com.woniu.mapper.MaintenancePersonMapper;
import com.woniu.util.CodeGeneratorUtil;
import com.woniu.util.PageData;
import com.woniu.util.ResponseData;
import com.woniu.util.easyExcelUtil.EasyExcelUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
* @author Apricity
* @description 针对表【maintenance_person(运维人员表)】的数据库操作Service实现
* @createDate 2025-02-13 20:16:59
*/
@Service
public class MaintenancePersonServiceImpl extends ServiceImpl<MaintenancePersonMapper, MaintenancePerson>
    implements MaintenancePersonService{

    @Resource
    private MaintenancePersonMapper maintenancePersonMapper;

    /*
    * 新增
    * */
    @Override
    public void savePerson(MaintenancePerson maintenancePerson) {
        // 生成用户编号
        String userCode = CodeGeneratorUtil.generateEmpLoyeeCode();
        maintenancePerson.setUserNumber(userCode);
        maintenancePerson.setStatus(CodeGeneratorUtil.PERSON_STATUS);
        maintenancePerson.setAvatarUrl(maintenancePerson.getAvatarUrl());

        // 插入数据库
        maintenancePersonMapper.insert(maintenancePerson);
    }

    /*
    * 删除
    * */
    @Override
    public void removePersonById(List<String> userNumber) {
        if (userNumber == null || userNumber.isEmpty()){
            throw new IllegalArgumentException("要删除人员集合中不能为空");
        }
        // 直接根据 ID 删除
        maintenancePersonMapper.deleteByUserNumber(userNumber);
    }

    /*
    * 修改
    * */
    @Override
    public void updatePersonById(MaintenancePerson maintenancePerson) {
        // 根据 ID 更新
        maintenancePersonMapper.updateById(maintenancePerson);
    }

    @Override
    public MaintenancePerson getbyPersonId(String userNumber) {
        MaintenancePerson maintenancePerson = maintenancePersonMapper.selectByuserNumber(userNumber);

        return maintenancePerson;
    }

    @Override
    public Page<MaintenancePerson> queryPage(PageData pageData) {
        Page<MaintenancePerson> page = new Page<>(pageData.getPageNum(), pageData.getPageSize());
        // MyBatis-Plus 分页查询，自动计算 total
        IPage<MaintenancePerson> MaintenancePerson = maintenancePersonMapper.selectPageAndParam(page, (String) pageData.getParam());
        return (Page<MaintenancePerson>) MaintenancePerson;
    }

    @Override
    public void updateStatus(Integer status, String userNumber) {
        MaintenancePerson person = new MaintenancePerson();
        person.setUserNumber(userNumber);
        person.setStatus(status);

        maintenancePersonMapper.updateStatus(person);
    }

    @Override
    public List<MaintenancePerson> getUserInfo(String userName) {
        if (userName != null&&!userName.trim().equals("")) {
            return maintenancePersonMapper.selectList(new QueryWrapper<MaintenancePerson>().like("user_name", userName));
        }
            return maintenancePersonMapper.selectList(null);
    }

    @Override
    public ResponseData<?> outDepartmentExcelTemplate(HttpServletResponse response) throws IOException {
        String fileName = "运维人员模板";

        // **模拟获取数据库数据**
        List<MaintenancePerson> dataList = getMaintenancePersons();

        // **设置 Excel 响应头**
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="
                + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");

        EasyExcelUtil.writeWithSheetsWeb(response, fileName)

                .writeModel(MaintenancePerson.class, dataList, "personName1")
                .finish();
        return ResponseData.ok();
    }

    private List<MaintenancePerson> getMaintenancePersons() {
        ArrayList<MaintenancePerson> list = new ArrayList<>();

        List<MaintenancePerson> maintenancePeople = maintenancePersonMapper.selectList(null);
        for (MaintenancePerson maintenancePerson : maintenancePeople) {
            list.add(maintenancePerson);
        }

        return list;
    }
}




